<?php
/**
 * 汇友科技
 * @copyright (c) 2017 http://www.huiyou365.net All rights reserved.
 * @author: Creatint <creatint@163.com>
 */

namespace Home\Model;

use Common\Model\Model;
use Think\Page;

class RecordModel extends Model{

    protected $_validate = array(
        array('mobile', '/^[0-9]{8,12}$/', '联系电话不正确', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        array('content', 'require', '问题描述不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_INSERT),
//        array('location', 'require', '位置信息不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_INSERT),
//        array('location', 'checkLocation', '位置信息不正确', self::MUST_VALIDATE, 'callback', self::MODEL_INSERT),
        array('content', '5,255', '内容需5字以上,或至少填写一条标题', self::MUST_VALIDATE, 'length', self::MODEL_BOTH),
//        array('question_id', 'require', '问题类型不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_INSERT),
//        array('question_id', 'checkQuestion', '问题类型不正确', self::MUST_VALIDATE, 'callback', self::MODEL_INSERT),
        array('chenghu', 'require', '称呼不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_INSERT),
    );

    protected $_auto = array(
        array('created_at', NOW_TIME, self::MODEL_INSERT),
        array('updated_at', NOW_TIME, self::MODEL_BOTH),
    );


    public function remove($id){
        if ( !$id ) {
            $this->error = '待删除项为空';
            return false;
        }
        $map = [
            'id'=>[
                'IN',
                $id
            ]
        ];
        if ( $this->where($map)->delete() ) {
            S('DB_CONFIG', null);
            return true;
        } else {
            return false;
        }
    }


    protected function checkQuestion($question_id){

        return true;
    }
    protected function checkLocation($location){

        return true;
    }


    /**
     * 检查标识是否已存在(只需在同一根节点下不重复)
     * @param string $name
     * @return true无重复，false已存在
     * @author huajie <banhuajie@163.com>
     */
    protected function checkName(){
        $name        = I('post.name');
        $category_id = I('post.category_id', 0);
        $id          = I('post.id', 0);

        $map = array('name' => $name, 'id' => array('neq', $id), 'status' => array('neq', -1));

        $category = get_category($category_id);
        if ($category['pid'] == 0) {
            $map['category_id'] = $category_id;
        } else {
            $parent             = get_parent_category($category['id']);
            $root               = array_shift($parent);
            $map['category_id'] = array('in', D("Category")->getChildrenId($root['id']));
        }

        $res = $this->where($map)->getField('id');
        if ($res) {
            return false;
        }
        return true;
    }

    /**
     * 生成推荐位的值
     * @return number 推荐位
     * @author huajie <banhuajie@163.com>
     */
    protected function getPosition(){
        $position = I('post.position');
        if(!is_array($position)){
            return 0;
        }else{
            $pos = 0;
            foreach ($position as $key=>$value){
                $pos += $value;		//将各个推荐位的值相加
            }
            return $pos;
        }
    }

    public function getPage(){
        return $this->page;
    }

}